package com.card.server.utils;

import com.card.server.domain.Card;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2025/6/2 23:44
 * @desc
 */
@Component
public class DeckUtil {
    // 创建一副牌（54张，含大小王）
    public static List<Card> createDeck() {
        List<Card> deck = new ArrayList<>();

        String[] decors = {"RedHearts", "Spades", "Square", "ThePlumBlossom"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        int[] sizes = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

        // 添加普通牌
        for (String decor : decors) {
            for (int i = 0; i < numbers.length; i++) {
                deck.add(new Card(decor, numbers[i], sizes[i]));
            }
        }

        // 添加大小王
        deck.add(new Card("joker", "XW", 14)); // 小王
        deck.add(new Card("joker", "DW", 15)); // 大王

        return deck;
    }

    // 洗牌并分组为4组
    public static List<List<Card>> splitDeckToGroups() {
        List<Card> deck = createDeck();
        Collections.shuffle(deck); // 洗牌

        List<List<Card>> groups = new ArrayList<>();

        // 前三组各17张
        for (int i = 0; i < 3; i++) {
            groups.add(new ArrayList<>(deck.subList(i * 17, (i + 1) * 17)));
        }

        // 最后一组3张
        groups.add(new ArrayList<>(deck.subList(51, 54)));

        return groups;
    }

    // 测试方法
    public static void main(String[] args) {
        List<List<Card>> groups = splitDeckToGroups();

        for (int i = 0; i < groups.size(); i++) {
            System.out.println("第 " + (i + 1) + " 组，共 " + groups.get(i).size() + " 张牌：");
            for (Card card : groups.get(i)) {
                System.out.print("  " + card.getDecor() + "_" + card.getNumber() + "_" + card.getSize());
            }
            System.out.println();
        }
    }
}
